package com.tensai.study.java.features.JDK8.date;

import org.junit.Test;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * 时间测试类
 *
 * @author Tensai
 */
public class InstantTest {


	/**
	 * 解析测试
	 * 必须是UTC时间 否则会报错
	 */
	@Test
	public void parseTest() {
//		Instant instant = Instant.parse("2021-09-03 10:50:28");
		Instant instant = Instant.parse("2007-12-03T10:15:30.00Z");
		System.out.println(instant.toString());
	}

	/**
	 * 解析测试
	 * 非UTC时间格式数据解析
	 */
	@Test
	public void parseByLocalDateTimeTest() {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime localDateTime0 = LocalDateTime.parse("2021-09-03 10:50:28", formatter);
		System.out.println(localDateTime0.toInstant(ZoneOffset.ofHours(8)));
	}

	/**
	 * Instant 不支持加减一个月/一年
	 */
	@Test
	public void testMinus() {
		Instant instant = Instant.parse("2000-01-01T00:00:00.00Z");
		LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
		for (int i = 0; i < 366; i++) {
			Instant instant1 = instant.plus(i, ChronoUnit.DAYS).minus(1, ChronoUnit.MONTHS);
			LocalDateTime localDateTime1 = LocalDateTime.ofInstant(instant1, ZoneId.systemDefault());
//			if (localDateTime1.getDayOfMonth() != localDateTime.getDayOfMonth()) {
			System.out.println(localDateTime + " -- " + localDateTime1);
//			}
		}
	}


}
